package com.quzi.kettle.activitise;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.quzi.kettle.R;
import com.quzi.kettle.json.JsonUtils;
import com.quzi.kettle.utils.HttpUtil;

public class HttpTest extends Activity {
	private TextView tView;
	private Button get, post;
	HttpUtil httpUtil;
	String urlStr = "http://121.42.15.85:8000/api/user/login";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_http);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		tView = (TextView) findViewById(R.id.textvie_http);
		get = (Button) findViewById(R.id.get);
		post = (Button) findViewById(R.id.post);
		get.setOnClickListener(listener);
		post.setOnClickListener(listener);
	}

	View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.get:

				tView.setText(getHttpUtil().get(urlStr));
				break;
			case R.id.post:

				Map<String, String> params = new HashMap<String, String>();
				params.put("phone_number", "18026759184");
				params.put("password", "222222");

				// "ua":"用户账号","dev":"设备编号","dname":"设备名称","duser":"设备用户","dpwd":"设备密码","dchanums":"通道数","gwflag":"网关标识""pushflag":"报警开关"
				String string = getHttpUtil().getRequestData(params, "utf-8")
						.toString();
				String jsonString = getHttpUtil().post(urlStr, string);
				System.out.println("----jsonString---->" + jsonString);
				Log.i("HttpTest", jsonString);
				tView.setText(JsonUtils.getPerson("person", jsonString)
						.toString());
			default:
				break;
			}

		}
	};

	private HttpUtil getHttpUtil() {
		if (httpUtil == null) {
			httpUtil = HttpUtil.getInstance();
		}
		return httpUtil;
	}
}
